<?php

/**
 * curl 取出绑定帐号信息
 */
class bind
{

    /*
    static $url = array(
        'sina_userinfo' => 'https://api.weibo.com/2/users/show.json',
        'sina_friends' => 'https://api.weibo.com/2/friendships/friends/ids.json'
    );

    static public function bindqq($openid, $access_token)
    {
        
    }

    static public function bindweibo($openid, $access_token)
    {
        $url = self::$url['sina_userinfo'];
        $query['uid'] = $openid;
        $query['access_token'] = $access_token;

        $info = common::getContents($url, $query);
        $sinaUserinfo = json_decode($info, true);
        $genderMap = array('m' => 1, 'f' => 2, 'n' => 0);

        if (isset($sinaUserinfo['error']))
        {
            return FALSE;
        }
        
        $bindInfo['nickname'] = $sinaUserinfo['name'];
        $bindInfo['header'] = $this->saveProfile($sinaUserinfo['profile_image_url']);
        $bindInfo['gender'] = $genderMap[$sinaUserinfo['gender']];
        return $bindInfo;
    }

    */
    /**
     * 保存球员对象
     * @param type $url
     */
    static function saveProfile($url)
    {
        $result = FALSE;

        $day = date('Ymd');
        $fix = 'jpg';
        $upPath = FILE . '../web/static/u/' . $day . '/';

        if (!is_dir($upPath))
        {
            mkdir($upPath);
        }
        $fileName = md5($url . microtime(TRUE)) . '.' . $fix;
        $upFileName = $upPath . $fileName;
        $fp = fopen($url, 'r');
        if (file_put_contents($upFileName, $fp))
        {
            $result = 'http://' . common::host() . '/static/u/' . $day . '/' . $fileName;
        }
        fclose($fp);
        return $result;
    }
}
